﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Engine.Gameplay.GameWorld;

namespace Engine.Game.Cameras
{
    public abstract class Camera : ICamera, IDisposable
    {
        #region Fields
        #endregion // Fields


        #region Properties
        public Vector3 Position { get; protected set; }
        public Vector3 LookAt { get; protected set; }
        public float AspectRatio { get; protected set; }

        public Matrix View { get; private set; }
        public Matrix Projection { get; private set; }
        #endregion // Properties


        #region Constructors
        public Camera(float aspectRatio)
        {
            AspectRatio = aspectRatio;
            View = Matrix.CreateLookAt(new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up);
            Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), AspectRatio, 1.0f, 1000.0f);
        }
        #endregion // Constructors


        #region Methods
        public void Update(GameTime gameTime)
        {
            UpdateCamera(gameTime);
            UpdateMatrices();
        }

        protected abstract void UpdateCamera(GameTime gameTime);


        public void UpdateMatrices()
        {
            Matrix rotation = Matrix.CreateRotationX(0.0f) * Matrix.CreateRotationY(0.0f);
            Vector3 transformedReference = Vector3.Transform(new Vector3(0, 0, -1), rotation);
            Vector3 LookAt = Position + transformedReference;

            View = Matrix.CreateLookAt(Position, LookAt, Vector3.Up);
        }

        public void Dispose()
        {
            Shutdown();
        }
        
        protected abstract void Shutdown();
        #endregion // Methods
    }
}
